<?php
class Jjjs
{
    /*
     * Jjjs::AllDayFhjjs();
     * @return bool 计算每天每个用户分红
     */
    static function AllDayFhjjs()
    {
        $model = new Model();
        $nowDay =  intval(strtotime(date("Y-m-d",time())));  //精确到天
        $now_h = date('H',time());                   //当天十一点分红
        if($now_h < 23)
        {
            return false1;
        }
        $now_i = date('i',time());
        if($now_i <30)
        {
            return false2;
        }

        $week = date("w");                       //每个星期只有周一和周五分红
        if(intval($week) ===0 || intval($week) === 6 )
        {
            return false3;
        }

        $AlluserObj_Dayff = $model ->table("user as u")->join("inner join customer as c on u.id =c.user_id ")->where("c.zc>0 and c.last_fhjf>0 and u.status =1 and u.jjtime <$nowDay " )->select();

        if(empty($AlluserObj_Dayff)){return false;}

        foreach($AlluserObj_Dayff as $userObj)
        {
            if(!empty($userObj['jh_time']) )
            {
                $strtotime = strtotime($userObj['jh_time']);
                $n_h = date('H',$strtotime);  //用户激活的 小时

                $n_i = date('i',$strtotime);  //获取用户激活的 分钟

                $n_d = date('d',$strtotime);  //获取用户激活的 天

                $Day = date("d",time());

                if($Day <=$n_d && $n_h >=23 && $n_i>=30 )  //当天十一点半以后的用户不发奖金
                {
                    continue;
                }

            }

            $nowDay1 =  intval(strtotime(date("Y-m-d",time())))-15;
            self::getDayFhByUserId($userObj['id']);  //用户每天分红

            $model->table("user")->where("id =".$userObj['id'])->data(array("jjtime" =>$nowDay1))->update();


        }

        $AlluserObj_Otherjj_1 = $model ->table("user as u")->join("inner join customer as c on u.id =c.user_id ")->where("c.zc>0 and u.status =1  and u.jjtime <$nowDay")->select();

        if(empty($AlluserObj_Otherjj_1)){return false;}

        foreach($AlluserObj_Otherjj_1 as $userObj_Other_1)
        {


            if(!empty($userObj['jh_time']) )
            {
                $strtotime = strtotime($userObj['jh_time']);
                $n_h = date('H',$strtotime);

                $n_i = date('i',$strtotime);

                $n_d = date('d',$strtotime);  //获取用户激活的 天

                $Day = date("d",time());

                if($Day <=$n_d && $n_h >=23 && $n_i>=30 )  //当天十一点半以后的用户不发奖金
                {
                    continue;
                }

            }
            $nowDay2 =  intval(strtotime(date("Y-m-d",time())))-10;
            self::zlj(intval($userObj_Other_1['id']));      //助力奖
            $model->table("user")->where("id =".$userObj['id'])->data(array("jjtime" =>$nowDay2))->update();
        }

        $AlluserObj_Otherjj_2 = $model ->table("user as u")->join("inner join customer as c on u.id =c.user_id ")->where("c.zc>0 and u.status =1  and u.jjtime <$nowDay")->select();

        if(empty($AlluserObj_Otherjj_2)){return false;}

        foreach($AlluserObj_Otherjj_2 as $userObj_Other_2)
        {
            if(!empty($userObj['jh_time']) )
            {
                $strtotime = strtotime($userObj['jh_time']);
                $n_h = date('H',$strtotime);

                $n_i = date('i',$strtotime);

                $n_d = date('d',$strtotime);  //获取用户激活的 天

                $Day = date("d",time());

                if($Day <= $n_d && $n_h >=23 && $n_i>=30 )  //当天十一点半以后的用户不发奖金
                {
                    continue;
                }

            }

            $nowDay3 =  intval(strtotime(date("Y-m-d",time())));
            self::Kzj(intval($userObj_Other_2['id']));      //扩展奖

            $model->table("user")->where("id =".$userObj_Other_2['id'])->data(array("jjtime" =>$nowDay3))->update();
        }

        return true;
    }


    /**
     * @return bool 奖金如果出错,追回奖金函数
     */
    static function AllDayFhjjs_back()
    {
        $model = new Model();
        $nowDay =  intval(strtotime(date("Y-m-d",time())));  //精确到天
        $AlluserObj_Dayff = $model ->table("user as u")->join("inner join customer as c on u.id =c.user_id ")->where("c.zc>0 and c.last_fhjf>0 and u.status =1  " )->select();

        if(empty($AlluserObj_Dayff)){return false;}

        foreach($AlluserObj_Dayff as $userObj)
        {

            self::getDayFhByUserId($userObj['id']);  //用户每天分红

        }

        $AlluserObj_Otherjj_1 = $model ->table("user as u")->join("inner join customer as c on u.id =c.user_id ")->where("c.zc>0 and u.status =1  ")->select();

        if(empty($AlluserObj_Otherjj_1)){return false;}

        foreach($AlluserObj_Otherjj_1 as $userObj_Other_1)
        {

            self::zlj(intval($userObj_Other_1['id']));      //助力奖

        }

        $AlluserObj_Otherjj_2 = $model ->table("user as u")->join("inner join customer as c on u.id =c.user_id ")->where("c.zc>0 and u.status =1  and u.jjtime <$nowDay")->select();

        if(empty($AlluserObj_Otherjj_2)){return false;}

        foreach($AlluserObj_Otherjj_2 as $userObj_Other_2)
        {


            $nowDay2 =  intval(strtotime(date("Y-m-d",time())));
            self::Kzj(intval($userObj_Other_2['id']));      //扩展奖

            $model->table("user")->where("id =".$userObj_Other_2['id'])->data(array("jjtime" =>$nowDay2))->update();
        }

        return true;
    }




    /**
     * $param 助力奖
     * @param $id
     * @return bool|mixed
     */
    static function zlj($id)
    {
        $zlj = null;
        $from ="助力奖";
        $type= 2;
        $userObj = Common::getUserInfoById($id);           //return $userObj;      //获取用户信息

        $dai = self::getZljDaiByUserId($id);                      //获取该用可获取几代助力奖;
        if(empty($dai)){return true;}

        $TjrList_array = Common::getTjrListByUserId($id, $dai);      //return $TjrList_array ;   //获取该代数的用户推荐链条;

        foreach ($TjrList_array as $count =>$value) {
            $count = $count+1;
            $min_fhjf = self::getSmallJf($userObj['id'], $value['id']);                      // return $min_fhjf;               //获取最小的分红积分

            if (intval($count) === 1) {
                $zlj_rate = intval(Common::getConfigJjsetValue('jjset_zlj_one')) * 0.01;          // return $zlj_rate;
            }
            if (intval($count) === 2) {

                $zlj_rate = intval(Common::getConfigJjsetValue('jjset_zlj_two_2')) * 0.01;      //var_dump($kzj_rate);
            }

            $zlj += $zlj_rate * $min_fhjf;

        }

        if(empty($zlj)){return true;}
        return self::zjf_divide($zlj,$from,$id,$type);
    }

    /**
     * @param $id
     * @param $fid
     * @return mixed 获取最小的每天分红积分
     */
    public static function getSmallJf($id, $fid)
    {
        $array = array();
        $model = new Model();
        $grade_id = $model->table("customer")->where("user_id =" . $id)->find()['vip_id']; //获取用户的对应级别
        $fGrade_id = $model->table("customer")->where("user_id =" . $fid)->find()['vip_id'];// return $grade_id;//获取推荐人的对应级别

        $array[] = Common::getDayFhByGradeId($grade_id);                      //激活用户的获取每天分红积分
        $array[] = Common::getDayFhByGradeId($fGrade_id);                                                        //return $array;
        return min($array);
    }

    /**
     * @param $id 分积分的用户的id
     * @return bool
     */
    static function getZljDaiByUserId($id)
    {
        $Tjrs=  Common::getUserValidTjrsById($id);

        if (intval($Tjrs) >= 2 && intval($Tjrs <= 4)) {
            $dai = 1;
        }
        if (intval($Tjrs) > 4) {
            $dai = 2;
        }
        return $dai;
    }


//-------------------------------------------------


    /**
     * @param $id 用户的id
     * @return 扩展奖
     */
    static function Kzj($id)
    {
        $Kzj = null;

        $type= 3;
        $userObj = Common::getUserInfoById($id);                  //获取用户信息

        $TjrList_array = Common::getTjrListByUserId($id, 3);  //return $TjrList_array;             //获取该用可获取几代拓展奖;

        foreach ($TjrList_array as $count=> $value)
        {
            $count = $count+1;

            //获取最小的分红积分
            $min_fhjf = self::getSmallJf($userObj['id'], $value['id']);

            $kzj_rate = intval(Common::getConfigJjsetValue('jjset_kzj'.$count)) * 0.01;

            $u_tjrs =Common::getUserValidTjrsById($value['id']);  //获取该用户有效的推荐人数
            if (intval($count) === 1 && $u_tjrs >=1) {                                                             //第一代 推荐人1-2人
                $from = $value['name']."获拓展奖 因:".$userObj['name']."获取分红奖";
                $kzj = $min_fhjf * $kzj_rate;

                if($kzj > 0)                                                         //
                {
                    self::zjf_divide($kzj,$from,$value['id'],$type);
                }
            }
            if (intval($count) === 2 && $u_tjrs >=3) {
                $from = $value['name']."获拓展奖 因:".$userObj['name']."获取分红奖";
                $kzj = $min_fhjf * $kzj_rate;

                if($kzj > 0)
                {
                    self::zjf_divide($kzj,$from,$value['id'],$type);
                }
            }
            if (intval($count) === 3 && $u_tjrs >= 5) {
                $from = $value['name']."获拓展奖 因:".$userObj['name']."获取分红奖";
                $kzj = $min_fhjf * $kzj_rate;

                if($kzj > 0)
                {
                    self::zjf_divide($kzj,$from,$value['id'],$type);
                }
            }


        }

        return true;

    }

    /**
     * @param $id  用户分红
     * @return bool 每天给用户分红
     */
    static function getDayFhByUserId($id)
    {

        $dayFh = Common::getDayFhByUserId($id);  //根据用户id获取 等级对应的每天分红
        $from = "每天分红";
        $type= 1;

        $last_fhjf = Common::getCustomerInfoById($id)['last_fhjf'];  //如果日分红大于等于 剩余分红积分时 按剩余分红积分分红
        if($last_fhjf <= $dayFh)
        {
            $dayFh = $last_fhjf;
        }

        return  self::zjf_divide($dayFh, $from,$id,$type) && self::DayFh_divide($dayFh,$id);
    }

    /**
     * @param $dayFh
     * @param $id
     * @return mixed
     */
    static function DayFh_divide($dayFh, $id)
    {
        $model = new Model();
        $data_customer = array(
            "last_fhjf" =>"`last_fhjf` -".$dayFh
        );
        $customer = $model ->table("customer")->data($data_customer)->where("user_id =".$id)->update();
        return $customer;
    }

    /**
     * @param $zjf  总积分
     * @param $from   奖金名称
     * $param $id   用户id
     * @return mixed 总积分被瓜分
     */
    static function zjf_divide($zjf, $from,$id,$type)
    {
        $model =new Model();

        $model->query("start transaction");                 //开启事物
        $customerInfo =   Common::getCustomerInfoById($id); //获取用户customer表中信息
        if($customerInfo['zc'] >= $zjf)                                 //当资产大于每天分红时
        {
            $zc = "`zc` -".$zjf;                        //return Common::getLimitZcByUserId($id); //用户资产


        }elseif($customerInfo['zc'] < $zjf)
        {
            $zjf = $customerInfo['zc'];
            $zc ="0";
        }
        $big_zc =Common::getInvestMoneyByUserId($id);   //获取投资金额

        $alljJ =  Common::get_jj_all_jjmix($id);     // return $alljJ;   $a = Common::getLimitZcByUserId($id);     //用户的所有奖金 : 每日分红,拓展奖和助力奖                                                                                  // return  $alljJ;

        if($alljJ >= $big_zc)
        {
            $axjj = $zjf*Common::getConfigJjsetValue("jjset_axjj")*0.01;       //爱心积分
            $zjf1 = $zjf - $axjj;
        }else{

            $axjj = 0;
            $zjf1 = $zjf;
        }

        if(intval($type) === 1)         //当前分红
        {
            $jl = "fhj";
        }elseif(intval($type) === 2)    //助力奖
        {
            $jl = "zlj";
        }elseif(intval($type) === 3)   //扩展奖
        {
            $jl = "kzj";
        }

        $cxjf = Common::getConfigJjsetValue("jjset_cxjf") * 0.01 * $zjf1;       //重消积分

        $zjf2 = $zjf1 - $cxjf;                                                 //扣除重消积分后的积分收入

        $jjf = Common::getConfigJjsetValue("jjset_jjf") * 0.01 * $zjf2;        //奖金分
        $zcf = Common::getConfigJjsetValue("jjset_zcf") * 0.01 * $zjf2;        //注册分
        $model = new Model();
        $intro ="总积分:".$zjf."扣除爱心积分: $axjj,重消积分: $cxjf,奖金分:$jjf,注册分:$zcf"; // return $axjj;
        $data_jjmx= array(
            "from" =>$from,
            "intro" =>"$intro",
            "cxjf" =>$cxjf,
            "axjj" =>$axjj,
            "jjf"  =>$jjf,
            "zcf"  =>$zcf,
            "jjtime"=>date('Y-m-d H:i:s'),
            "user_id"=>$id,
            $jl   =>$zjf
        );
        $jjmx = $model ->table("jjmx")->data($data_jjmx)->insert(); //return $jjmx;
        $data_customer = array(
            "axjj" =>"`axjj`+".$axjj,
            "cxjf" =>"`cxjf`+".$cxjf,
            "jjf"  =>"`jjf`+".$jjf,
            "zcf"  =>"`zcf`+".$zcf,
            "zc"     =>$zc
        );
        $customer = $model ->table("customer")->data($data_customer)->where("user_id =".$id)->update();

        if(!empty($jjmx) && !empty($customer))
        {
            $model->query("commit");
            return $jjmx && $customer;
        } else{
            $model ->query("rollback");
            return false;
        }


    }
}
?>